package com.gopay.bis.hnapay.ebank.impl;

import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.hnapay.ebank.operation.HnaPayCertHelperQuery;
import com.gopay.common.constants.bank.HnaPayConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.hnapay.ebank.HnaPayMessage;
import com.gopay.common.domain.bank.hnapay.ebank.query.HnaPayMessageQuery;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpsTransport;
import com.gopay.remote.bank.ICertHelper;
import com.gopay.remote.bank.businesses.BankService;
/**
 * 新生在线网银直连接口处理
 * 
 * @HnaPayBankServiceImpl.java
 * @author LiLiLi
 */
public class HnaPayBankServiceImpl implements BankService {
	private ICertHelper hnaPayCerthelper;
	
	private HnaPayCertHelperQuery queryCerthelper;
	
	private HttpsTransport httpsTransport;
	
	
	public Object sign(BaseMessage msg) throws GopayException {
		String signMessage = (String)hnaPayCerthelper.sign(msg);
		return signMessage;
	}

	public Object verify(BaseMessage msg) throws GopayException {
		boolean verify = false;
		HnaPayMessage message = (HnaPayMessage)msg;
		verify = (Boolean)hnaPayCerthelper.verify(message);
		return verify;
	}

	public Object query(Object obj) throws GopayException {
		HnaPayMessageQuery message = (HnaPayMessageQuery)obj;
		try{
			String signMsg = (String) hnaPayCerthelper.sign(message);
			message.setSignMsg(signMsg);
			// 组装发送订单查询请求信息
			Map<String, String> reqMap = queryCerthelper
					.buildQueryMsg(message);
			httpsTransport
					.setUrl(HnaPayConstants.QUERY_URL); // 设置URL
			Object resObj = httpsTransport.submit(reqMap); // 发送请求
			HnaPayMessageQuery messageQuery = this.queryCerthelper.parseQueryMsg(resObj); // 返回结果解析
			if(messageQuery.getResultCode().equals("0000") || messageQuery.getResultCode().equals("0009")){
				messageQuery.setOrgCode(message.getOrgCode());
				messageQuery.setPlainMessage(this.queryCerthelper.buildPlain(messageQuery));
				// 验签
				boolean resFlag = (Boolean) hnaPayCerthelper.verify(messageQuery);
				if(resFlag){
					return messageQuery;
				}else{
					return null;
				}
		    }else{
		    	return messageQuery;
		    }
			
		}catch(Exception e){
			BankLogUtils.printB2CQueryExceptionLog(message, e);
			return null;
		}
		
	}

	public void setHnaPayCerthelper(ICertHelper hnaPayCerthelper) {
		this.hnaPayCerthelper = hnaPayCerthelper;
	}

	public void setQueryCerthelper(HnaPayCertHelperQuery queryCerthelper) {
		this.queryCerthelper = queryCerthelper;
	}

	public void setHttpsTransport(HttpsTransport httpsTransport) {
		this.httpsTransport = httpsTransport;
	}

	@Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}


}
